package com.oauth.google.server.global;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oauth.google.client.beans.orkut.OrkutBirthdayDetails;
import com.oauth.google.server.service.OrkutOAuthService;
import com.oauth.google.server.shared.orkut.OrkutUtil;
import com.oauth.logging.OAuthLogger;
import com.oauth.service.global.ApplicationGlobalServiceAdapter;
import com.oauth.service.global.beans.Birthday;
import com.oauth.service.global.beans.OAuthAccessToken;
import com.oauth.service.global.beans.OAuthRequestToken;

@Service
public class OrkutGlobalService extends ApplicationGlobalServiceAdapter {

	private OAuthLogger logger = OAuthLogger.getLogger(this.getClass().getName());

	@Autowired
	private OrkutOAuthService mOrkutOAuthService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oauth.service.application.global.ApplicationGlobalServiceAdapter#
	 * updateStatus(java.lang.Long, java.lang.String)
	 */
	@Override
	public boolean updateStatus(Long pMemberId, String pStatusText) {
		try {
			mOrkutOAuthService.updateStatus(pStatusText, pMemberId);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oauth.service.application.global.ApplicationGlobalServiceAdapter#
	 * getUpcomingBirthdays(java.lang.Long, int)
	 */
	@Override
	public List<Birthday> getUpcomingBirthdays(Long pMemberId, int pBirthdayInterval) {
		OrkutBirthdayDetails birthdayList;
		try {
			birthdayList = mOrkutOAuthService.getUpcomingBirthdays(pMemberId);
			logger.debug("Got birthday list from orkut=" + birthdayList.getCount() + "for member="
					+ pMemberId);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Birthday>();
		}
		return OrkutUtil.getGlobalBirthdays(birthdayList);
	}

	/**
	 * @return
	 * @throws Exception
	 * @see com.oauth.facebook.server.service.FacebookService#getAccessToken()
	 */
	public OAuthAccessToken getAccessToken(OAuthRequestToken requestToken) {
		try {
			return mOrkutOAuthService.getAccessToken(requestToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @return
	 * @throws Exception
	 * @see com.oauth.facebook.server.service.FacebookService#getRequestToken()
	 */
	public OAuthRequestToken getRequestToken() {
		try {
			return mOrkutOAuthService.getRequestToken();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param pOrkutOAuthService
	 *            the orkutOAuthService to set
	 */
	public void setOrkutOAuthService(OrkutOAuthService pOrkutOAuthService) {
		mOrkutOAuthService = pOrkutOAuthService;
	}

}
